package com.wwwarehouse.resource_center.fragment.createobject.maintainpic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wwwarehouse.common.BuildConfig;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.eventbus_event.MaintainGoodsImageEvent;
import com.wwwarehouse.resource_center.R;

import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import de.greenrobot.event.EventBus;


@Route(path = "/ResourceCenter/PicTimeFragment")
public class PicTimeFragment extends BaseFragment implements View.OnClickListener {
    private TextView mTimeTv;
    private Timer timer;
    private TimerTask timerTask;
    private long cnt = 0;
    private TextView mAddress;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof PicTimeFragment)
            mActivity.setTitle("维护商品图片");

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.res_duration_time_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        mTimeTv = (TextView) view.findViewById(R.id.time_task_view);
        mAddress = (TextView) view.findViewById(R.id.tv_address);
        TextView mAddress = (TextView) view.findViewById(R.id.tv_address);
        if (BuildConfig.AppName.equals(Constant.WANGCANG3)) {
            mAddress.setText("www.wwwarehouse.com");
        } else if (BuildConfig.AppName.equals(Constant.VORES)) {
            mAddress.setText("w3p.vores.com");
        }
        
        view.findViewById(R.id.btn_confirm).setOnClickListener(this);
        timer = new Timer();
        startClick();

    }

    @Override
    public void requestDatas() {
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_confirm) {
            EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
        }
    }


    /**
     * 格式化时间
     *
     * @param cnt
     * @return
     */
    private String getStringTime(long cnt) {
        long hour = cnt / 3600;
        long min = cnt % 3600 / 60;
        long second = cnt % 60;
        return String.format(Locale.CHINA, "%02d:%02d:%02d", hour, min, second);
    }


    /**
     * 开始计时
     */
    public void startClick() {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTimeTv.setText(getStringTime(cnt++));

                    }
                });
            }
        };
        timer.schedule(timerTask, 0, 1000);
    }


    /**
     * 接收推送消息，退出页面。
     */
    public void onEventMainThread(MaintainGoodsImageEvent maintainGoodsImageEvent) {
        if (peekFragment() instanceof PicTimeFragment) {
            toast("任务完成");
            EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
